//
//  GQHTruckInfoController.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHTruckModel.h"

#pragma mark Object

#pragma mark View
#import "GQHTruckInfoView.h"

#pragma mark Controller
#import "GQHTruckInfoController.h"
#import "GQHPushFreightsController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHTruckInfoController () <GQHTruckInfoViewDelegate> {
    
#pragma mark Data
    /// 车辆详情模型
    GQHTruckModel *truckModel;
#pragma mark View
    /// 自定义根视图
    GQHTruckInfoView *rootView;
}

@end


@implementation GQHTruckInfoController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHTruckInfoView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchServiceData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"车辆详情";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_followedOrUnfollowed:(UIButton *)sender {
    DLog(@"关注或取消关注");
    
    [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
        
        [self followedOrUnfollowed];
    } unallowed:nil];
}

- (void)qh_dial:(UIButton *)sender {
    DLog(@"拨打电话");
    
    [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
        
        [self qh_dialWithMobile:truckModel.qh_driverMobile];
    } unallowed:nil];
}

- (void)qh_checkMoreRoutes:(UIButton *)sender {
    DLog(@"更多路线");
    
}

- (void)qh_checkMoreReviews:(UIButton *)sender {
    DLog(@"更多评论");
    
}

- (void)qh_pushFreight:(UIButton *)sender {
    DLog(@"推送货源");
    
    GQHPushFreightsController *pushFreightsController = [[GQHPushFreightsController alloc] init];
    pushFreightsController.qh_truckModel = truckModel;
    pushFreightsController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pushFreightsController animated:YES];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceData {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if ([_qh_freeTruck isEqualToString:@"YES"]) {
        
        /// 空车详情
        [parameters setObject:_qh_truckID forKey:@"id"];
        [parameters setObject:USER_GET_ID forKey:@"token"];
        
        [GQHTruckModel qh_fetchFreeTruckInfoWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHTruckModel *truck) {
            DLog(@"%@-%@",message,truck);
            
            if (status) {
                
                truckModel = truck;
                rootView.qh_truckModel = truck;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
    } else {
        
        /// 关注的车辆详情
        [parameters setObject:_qh_truckID forKey:@"userId"];
        
        [GQHTruckModel qh_fetchFollowedTruckInfoWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHTruckModel *truck) {
            DLog(@"%@-%@",message,truck);
            
            if (status) {
                
                truckModel = truck;
                rootView.qh_truckModel = truck;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
    }
}

- (void)followedOrUnfollowed {
    
    if (truckModel.qh_followed) {
        
        /// 取消关注
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buUsersAttention/1/delete"];
        NSDictionary *parameters = @{@"beFocusOn" : truckModel.qh_driverID,@"focusOnPeople" : USER_GET_ID};
        
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            
            if (0 == [code intValue]) {
                
                [self fetchServiceData];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
    } else {
        
        /// 关注
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buUsersAttention/1/save"];
        NSDictionary *parameters = @{@"beFocusOn" : truckModel.qh_driverID,@"focusOnPeople" : USER_GET_ID};
        
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            
            if (0 == [code intValue]) {
                
                [self fetchServiceData];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
    }
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
